# -*- coding: utf-8 -*-

from django.shortcuts import render_to_response, get_object_or_404, render
from django.http import HttpResponseRedirect
from knowledge.models import Commonsense

# felix's work 2012.08.14
from itertools import chain
from operator import attrgetter

def index(request):

	latest_commonsense_list = Commonsense.objects.all()
	latest_chineseidiom_list = Chineseidiom.objects.all()

	# felix's work 2012.08.14
	# 서로다른 두 모델을 결합 itertool의 chain() 이용  http://docs.python.org/library/itertools.html
	latest_combined_list = sorted(chain(latest_commonsense_list,latest_chineseidiom_list), key=attrgetter('pub_date')
	
	# instance 확인 방법  eg)  if isinstance( latest_combined_list[0], Commonsense) :
		
	
	return render_to_response('knowledge/index.html', {'latest_combined_list': latest_combined_list})

def cs_detail(request, commonsense_id):
	commonsense = get_object_or_404(Commonsense, pk = commonsense_id)

	return render_to_response('knowledge/cs_detail.html', {'commonsense': commonsense}) 

def ci_detail(request, chineseidiom_id):
	chineseidiom = get_object_or_404(Chineseidiom, pk = chineseidiom_id)

	return render_to_response('knowledge/ci_detail.html', {'chineseidiom': chineseidiom}) 